<?php
include_once("../initial.php");
include_once("../driver/drive.php");
include_once("../driver/tableLocation.php");
include_once("../global/global_function.php");
class orderRHandle
{
    public function get_all_data($orderby="", $limit="")
    {
        $tablename = tableLocation::commitTable();
        $sql = "select * from $tablename where $orderby $limit";
        return drive::get_all_rows($sql);
    }
    public function get_order_num($array)
    {
        $tablename = tableLocation::commitTable();
        $sql = get_select_sql($tablename, "count(*)", $array);
        $result = drive::get_one_value($sql);
        return $result;
    }
    public function get_some_data($array, $orderby='', $limit='')
    {
        $tablename = tableLocation::commitTable();
        $sql = "SELECT * FROM $tablename WHERE ";
        $first=0;
        foreach($array as $key=>$value)
        {
            if($first==0)
            {
                $sql .= " `$key`='$value' ";
                $first=1;
            }
            else
            {
                $sql .= "and `$key`='$value'";
            }
        }
        $sql .= " $orderby $limit";
        echo $sql;
        return drive::get_all_rows($sql);
    }
    public function searchDevice($uid, $cid, $type='')
    {
    		$user_course = tableLocation::userCourseTable();
    		$course = tableLocation::courseTable();
    		$device_remain=tableLocation::courseTable();
    		$device_course=tableLocation::deviceCourseTable();
    		$device=tableLocation::deviceTable();
    		$sql="select uc.booktime, dc.did, dc.cid, $device.* 
    					from $user_course as uc join $device_course as dc join $device 
    					where uc.uid='$uid' and uc.cid='$cid' and uc.cid=dc.cid and dc.did=$device.id and uc.booktime>0";
    		if($type==-1 || $type=='')
    		{
    				$sql = $sql." and $device.type>=0";
    		}		
    		else
    		{
    				$sql = $sql." and $device.type=$type";
    		}
    		return drive::get_all_rows($sql);
    }
    public function getRemainData($did, $beginDate, $endDate)
    {
    		$device_remain = tableLocation::deviceRemainTable();
    		$sql = "select remain from $device_remain where did='$did' and date>='$beginDate' and date<='$endDate'";
    		return drive::get_all_rows($sql);
    }
    public function getRemainAllInfo($did, $beginDate, $endDate)
    {
    		$device_remain = tableLocation::deviceRemainTable();
    		$sql = "select * from $device_remain where did='$did' and date>='$beginDate' and date<='$endDate'";
    		return drive::get_all_rows($sql);
    }
    public function getOrdersNumberByUserId($uid, $pass='')
    {
    		$commitTable = tableLocation::commitTable();
    		$sql = "SELECT id 
								FROM $commitTable 
								WHERE uid =  '$uid' ";
				if($pass!='')
				{
						$sql = $sql." AND pass='$pass'";
				}
				$result = drive::get_all_rows($sql);
				return count($result);
    }
    public function getOrdersByUserId($uid, $pass='', $limit='')
    {
    		$commitTable = tableLocation::commitTable();
    		$courseTable = tableLocation::courseTable();
    		$userTable = tableLocation::userTable();
    		$deviceTable = tableLocation::deviceTable();
    		$sql = "SELECT distinct cm.*, dt.name as dname, ut.name as uname, ct.name as cname, dt.barcode 
								FROM $commitTable AS cm 
								JOIN $courseTable AS ct 
								JOIN $userTable AS ut 
								JOIN $deviceTable AS dt 
								WHERE cm.uid =  '$uid' 
								AND cm.uid = ut.id 
								AND cm.cid = ct.id 
								AND cm.did = dt.id ";
				if($pass!='')
				{
						$sql = $sql." AND cm.pass='$pass' ";
				}
				$sql = $sql."	order by cm.createdate desc $limit";
				return drive::get_all_rows($sql);
    }
    
    //function for admin
    public function getOrdersNumberByAdminId($adminId)
    {
    		$commitTable = tableLocation::commitTable();
    		$channelDevice=tableLocation::channelDeviceTable();
    		$channelUserTable=tableLocation::channelUserTable();
    		$courseTable = tableLocation::channelUserTable();
    		$userTable = tableLocation::userTable();
    		$deviceTable = tableLocation::deviceTable();
    		$sql = "SELECT distinct cm.id 
								FROM $channelUserTable AS cu 
								JOIN $channelDevice AS cd 
								JOIN $commitTable AS cm 
								WHERE cu.uid =  '$adminId' 
								AND cu.chid = cd.chid 
								AND cd.did = cm.did";
				$result = drive::get_all_rows($sql);
				echo $sql;
				return count($result);
    }
    public function getOrdersByAdminId($adminId, $limit='')
    {
    		$commitTable = tableLocation::commitTable();
    		$channelDevice=tableLocation::channelDeviceTable();
    		$channelUserTable=tableLocation::channelUserTable();
    		$courseTable = tableLocation::courseTable();
    		$userTable = tableLocation::userTable();
    		$deviceTable = tableLocation::deviceTable();
    		$sql = "SELECT distinct cm.*, dt.name as dname, ut.name as uname, ct.name as cname, dt.barcode 
								FROM $commitTable AS cm 
								JOIN $channelUserTable AS cu 
								JOIN $channelDevice AS cd 
								JOIN $courseTable AS ct 
								JOIN $userTable AS ut 
								JOIN $deviceTable AS dt 
								WHERE cu.uid =  '$adminId' 
								AND cu.chid = cd.chid 
								AND cd.did = cm.did 
								AND cm.uid = ut.id 
								AND cm.cid = ct.id 
								AND cm.did = dt.id 
								order by cm.createdate desc 
								$limit";
				echo $sql;
				return drive::get_all_rows($sql);
    }
    public function searchByKeyword($keyword,  $beginDate, $endDate, $type, $adminId, $limit='')
    {
    		$commitTable = tableLocation::commitTable();
    		$channelDevice=tableLocation::channelDeviceTable();
    		$channelUserTable=tableLocation::channelUserTable();
    		$courseTable = tableLocation::courseTable();
    		$userTable = tableLocation::userTable();
    		$deviceTable = tableLocation::deviceTable();
    		$sql = "SELECT distinct cm.*, dt.name as dname, ut.name as uname, ct.name as cname, dt.barcode 
								FROM $commitTable AS cm 
								JOIN $channelUserTable AS cu 
								JOIN $channelDevice AS cd 
								JOIN $courseTable AS ct 
								JOIN $userTable AS ut 
								JOIN $deviceTable AS dt 
								WHERE cu.uid =  '$adminId' 
								AND cu.chid = cd.chid 
								AND cd.did = cm.did 
								AND cm.uid = ut.id 
								AND cm.cid = ct.id 
								AND cm.did = dt.id ";
				if($keyword!='')
				{
						$sql .= "	AND (dt.name like '%$keyword%' 
										or dt.barcode like '%$keyword%' 
										or cm.uid like '%$keyword%'
										or ct.name like '%$keyword%'
										or ut.name like '%$keyword%')";
				}
				if($beginDate!='')
				{
						$sql .= " AND cm.start_date='$beginDate' ";
				}
				if($endDate!='')
				{
						$sql .= " AND cm.end_date='$endDate' ";
				}
				if($type>=0 && $type<=2)
				{
						$sql .= " AND cm.pass='$type' ";
				}
				$sql .= " order by cm.createdate desc $limit";
				return drive::get_all_rows($sql);
    }
    public function getOrdersByUid($uid, $limit)
    {
    		$commitTable = tableLocation::commitTable();
    		$sql = "select * from $commitTable where uid='$uid' order by createdate desc $limit";
    		return drive::get_all_rows($sql);
    }
    public function getOrdersByDate($date, $limit)
    {
    		$commitTable = tableLocation::commitTable();
    		$sql = "select * from $commitTable where start_date<='$date' and end_date>='$date' order by createdate desc $limit";
    		return drive::get_all_rows($sql);
    }
}
?>